<template>
    <view class="page-community-index">
		<u-sticky>
			<!-- 只能有一个根元素 -->
			<view class="search-sticky">
				<u-row gutter="0">
					<u-col span="4" v-for="(item,index) in tab.list" :key="index">
						<view class="text-center">
                            <view class="inline-block" :class="tab.current == index ? 'current' : ''" @click="changeTab(index)">
                                <text class="name">{{ item.name }}</text>
                            </view>
                        </view> 
					</u-col>
				</u-row>
			</view>
		</u-sticky>
        <view class="p-4" v-if="tab.list[tab.current].records.length == 0">
            <u-empty text="暂无内容" mode="list"></u-empty>
        </view> 
        <view class="album pt-4">
            <view class="item" v-for="(item,index) in tab.list[tab.current].records" :key="index" @click="proxy.$util.gotoPage('/packageB/creation/detail', {id: item.id})">
                <view class="avatar">
                    <u-image width="84rpx" height="84rpx" :src="proxy.$util.getFullImage(item.avatar)" shape="circle"></u-image>
                </view>
                <view class="content">
                    <view class="name">{{ item.createBy }}</view>
                    <view class="text">
                        <!-- {{ item.content }} -->
                        <view v-html="proxy.$util.resetHtml(item.content)"></view>
                    </view>
                    <view class="comment">
                        <view class="time">{{ item.createTime }}</view>
                        <view class="btns">
                            <view class="btn-item" @click.stop="handleLike(item)"><u-image :src="item.yesNoLikes ? proxy.$icon.likePre : proxy.$icon.likeDef" width="42rpx" height="42rpx"></u-image><text class="ml-1">{{ item.likesSum }}</text></view>
                            <view class="btn-item" @click.stop="handleFavorite(item)"><u-image :src="item.yesNoFavorites ? proxy.$icon.collectPre : proxy.$icon.collectDef" width="42rpx" height="42rpx"></u-image><text class="ml-1">{{ item.favoritesSum }}</text></view>
                            <view class="btn-item" @click="proxy.$util.gotoPage('/packageB/creation/detail', {id: item.id})"><u-image :src="proxy.$icon.talk" width="42rpx" height="42rpx"></u-image><text class="ml-1">{{ item.commentsSum }}</text></view>
                            <!-- <view class="btn-item"><u-image :src="proxy.$icon.share" width="42rpx" height="42rpx"></u-image><text class="ml-1">999</text></view> -->
                        </view>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>
<script setup lang="ts">
import {onShow, onPullDownRefresh, onReachBottom } from '@dcloudio/uni-app';
import { useAppStore } from '@/stores/app';
const appStore = useAppStore();
import { useArticleStore } from '@/stores/article';
const articleStore = useArticleStore();
const {proxy} = getCurrentInstance() as ComponentInternalInstance;
let tab = reactive({
	current: 0,
	list:[
		// {id:null, name: '全部', records: []},
		{id:1, name: '笔记', records: []},
		{id:2, name: '推书', records: []},
		{id:0, name: '长文', records: []},
	]
});
const changeTab = (i) => {
	tab.current = i;
    if(tab.list[tab.current].records.length == 0){
        getList('refresh');
    }
}

const getList = (type) => {
    let obj = tab.list[tab.current];
      let params = {
        articlesType: obj.id,
      };
      proxy.$http('get', '/admin/favorites/currentUser', proxy.$util.getParam(params) ,(data,msg) => {
        obj.records =data;
      },msg => {

      })
};
const handleLike = (item) => {
    if(item.yesNoLikes){
        articleStore.deleteLike(item.id).then(res => {
            getList('refresh');
        });
    }else{
        let params = {
            articleId: item.id,
            userId: appStore.userInfo.userId,
        }
        articleStore.handleLike(params).then(res => {
            getList('refresh');
        });
    }
};
const handleFavorite = (item) => {
    if(item.yesNoFavorites){
        articleStore.deleteFavorite(item.id).then(res => {
            getList('refresh');
        });
    }else{
        let params = {
            articleId: item.id,
            userId: appStore.userInfo.userId,
        }
        articleStore.handleFavorite(params).then(res => {
            getList('refresh');
        });
    }
};
onPullDownRefresh(() => {
	getList('refresh');
	setTimeout( () => {
		uni.stopPullDownRefresh();
	}, 1000);
});
onShow(() => {
    getList(1);    
});
</script>
<style lang="scss">
page{background:#fff;}
</style>